<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图像生成</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/seg_obj_style.css') }}">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', sans-serif;
            background-color: #f7f6f4;
            padding: 30px 0;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: #f9f7f4;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0);
            background-color: rgba(0, 0, 0, 0); /* 完全透明 */
            padding: 40px 60px;
        }

        header {
            text-align: center;
            margin-bottom: 30px;
        }

        header h1 {
            font-size: 36px;
            color: black;
        }

        header p {
            font-size: 18px;
            color: black;
            margin-top: 10px;
        }

        .main-content {
            display: flex;
            justify-content: space-between;
            gap: 30px;
            background-color:white
        }

        .input-section, .output-section {
            width: 48%;
            padding: 20px;
            background-image: linear-gradient(to right,white);
            border-radius: 10px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        .input-section h3, .output-section h3 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            text-align: center;
        }

        .input-section textarea {
            width: 100%;
            height: 150px;
            padding: 12px;
            font-size: 16px;
            border-radius: 8px;
            border: 1px solid #ddd;
            margin-bottom: 20px;
            resize: none;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        .input-section button {
            width: 100%;
            padding: 12px;
            font-size: 18px;
            background-color: white; /* 绿色按钮 */
            color: black;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
            margin-left: -8px;
        }

        .input-section button:hover {
            background-color: gery; /* 悬停时变为亮绿色 */
        }

        .output-section img {
            width: 100%;
            max-height: 350px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }

        .loader {
            border: 8px solid #f3f3f3;
            border-top: 8px solid #3498db;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 2s linear infinite;
            display: none;
            margin: 20px auto;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .output-section button {
            padding: 12px;
            font-size: 16px;
            background-color: grey; /* 绿色按钮 */
            color: black;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            display: none;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
        }

        .output-section button:hover {
            background-color: grey; /* 悬停时变为亮绿色 */
        }
        #textInput {
            background-color: white; /* 淡橙色背景，透明度为0.5 */
            padding: 10px; /* 内边距 */
            width: 100%; /* 宽度 */
            height: 100%; /* 高度 */
        {#box-sizing: border-box; /* 边框和内边距包含在宽度和高度内 */#}
            resize: none; /* 禁止调整大小 */
        }
    </style>
</head>
<body>

<!-- 主容器 -->
<div class="container">
    <!-- 页头 -->
    <header>
        <h1>图像生成</h1>
        <p>输入描述并生成图像</p>
    </header>

    <!-- 主内容 -->
    <div class="main-content">
        <!-- 左侧：输入描述 -->
        <div class="input-section">
            <h3>输入描述</h3>
            <!-- 将表单包装起来并使用POST提交 -->
            <form id="generateForm">
                <textarea id="textInput" name="description" placeholder="请输入描述..." rows="5" cols="40"></textarea>
                <button type="submit">生成图像</button>
            </form>
        </div>

        <!-- 右侧：生成的图像 -->
        <div class="output-section">
            <h3>生成结果</h3>
            <img id="generatedImage" src="" alt="生成的图像">
            <div class="loader" id="loader"></div>
            <button>下载生成的图像</button>
        </div>
    </div>
</div>

<script>
    const generateForm = document.getElementById("generateForm");
    const loader = document.getElementById("loader");
    const generatedImage = document.getElementById("generatedImage");

    generateForm.addEventListener("submit", function(event) {
        event.preventDefault();  // 阻止表单的默认提交

        const desc = document.getElementById("textInput").value.trim();

        if (!desc) {
            alert("请输入描述！");
            return;
        }

        loader.style.display = "block";
        generatedImage.style.display = "none";

        fetch("/generate", {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({ description: desc })
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error("生成图像失败");
                }
                return response.json();
            })
            .then(data => {
                if (data.image_url) {
                    const imageUrl = data.image_url;
                    generatedImage.src = imageUrl;  // 设置图像路径

                    // 等待 2 秒再显示图像，同时隐藏加载动画
                    setTimeout(() => {
                        loader.style.display = "none";  // 隐藏加载动画
                        generatedImage.style.display = "block";  // 显示图像
                    }, 1000);
                }
            })
            .catch(error => {
                console.error("生成图像失败:", error);
                loader.style.display = "none";
                alert("生成图像失败，请重试。");
            });
    });

</script>

</body>
</html>
